package com.framework.cat.filter;

import com.dianping.cat.Cat;
import com.dianping.cat.CatConstants;
import com.framework.cat.constants.CatHttpConstants;
import com.framework.cat.context.CatContext;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.metadata.MetadataService;
import org.apache.dubbo.rpc.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashSet;
import java.util.Set;

/**
 *
 * @author dongsufeng
 * @date 2021/11/22 14:24
 * @version 1.0
 */

@Activate(group = "provider")
public class DubboProviderFilter implements Filter {

	private static final Logger log = LoggerFactory.getLogger(DubboConsumerFilter.class);
	public static Set<String> EXCLUDES = new HashSet();
	static {
		EXCLUDES.add(MetadataService.class.getName());
	}
	@Override
	public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
		if (EXCLUDES.contains(invoker.getInterface().getName())) {
			return invoker.invoke(invocation);
		}
		Cat.Context catContext = new CatContext();
		RpcContext context = RpcContext.getServerAttachment();
		catContext.addProperty(Cat.Context.ROOT, context.getAttachment(CatHttpConstants.CAT_HTTP_HEADER_ROOT_MESSAGE_ID));
		catContext.addProperty(Cat.Context.PARENT, context.getAttachment(CatHttpConstants.CAT_HTTP_HEADER_PARENT_MESSAGE_ID));
		catContext.addProperty(Cat.Context.CHILD, context.getAttachment(CatHttpConstants.CAT_HTTP_HEADER_CHILD_MESSAGE_ID));
		Cat.logRemoteCallServer(catContext);
		return LogServer.log(invoker,invocation,CatConstants.TYPE_SERVICE);
	}
}
